using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Noix.IMAP;

namespace Noix.TestIMAP
{
    public partial class Form1 : Form
    {
        private IMAPClient imap;
        private StringBuilder sb;
        
        public Form1()
        {
            InitializeComponent();
            imap = new IMAPClient();
            imap.ReceiveMessage += new ReceiveIMAPMessageDelegate(ProcessReceivedIMAPMessage);
        }

        private void ProcessReceivedIMAPMessage(object sender, IMAPMessageEventArgs e)
        {
            sb.AppendLine(e.message.Subject);
            tbMessages.Text += e.message.Subject;
            tbMessages.Text += Environment.NewLine;
        }

        private void bConnect_Click(object sender, EventArgs e)
        {
            try
            {
                imap.Connect(tbHost.Text, Convert.ToInt32(tbPort.Text));
            }
            catch (Exception exeption)
            {
                MessageBox.Show(exeption.ToString());
            }
        }

        private void bAuthenticate_Click(object sender, EventArgs e)
        {
            try
            {
                imap.Authenticate(tbUser.Text, tbPassword.Text);
            }
            catch (Exception exeption)
            {
                MessageBox.Show(exeption.ToString());
            }
        }

        private void bDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                imap.Disconnect();
            }
            catch (Exception exeption)
            {
                MessageBox.Show(exeption.ToString());
            }
        }

        private void bSelectFolder_Click(object sender, EventArgs e)
        {
            try
            {
                imap.SelectFolder(tbFolder.Text);
            }
            catch (Exception exeption)
            {
                MessageBox.Show(exeption.ToString());
            }
        }

        private void bFetch_Click(object sender, EventArgs e)
        {
            try
            {
                sb = new StringBuilder();
                imap.Fetch(1, -1, false);
                tbMessages.Text = sb.ToString();
            }
            catch (Exception exeption)
            {
                MessageBox.Show(exeption.ToString());
            }
        }
    }
}